﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using Lswweb.Model;
using Lswweb.IDAL;
using Lswweb.Common;

namespace Lswweb.SqlServerDAL
{
    public class NewsletterDAL:INewsletterDAL
    {
        #region 字段、构造方法、数据映射

        private Builder<NewsletterInfo> builder = new Builder<NewsletterInfo>("Newsletter", "Id");

        public NewsletterDAL(){
            this.builder.CreateModel += new ReaderBind<NewsletterInfo>(builder_CreateModel);
            this.builder.GetParameters += new ParametersBuilder<NewsletterInfo>(builder_GetParameters);
        }

        IDataParameter[] builder_GetParameters(NewsletterInfo t){

            return new IDataParameter[]{
                DataAccess.CreateParameter("@Id",t.Id),
                DataAccess.CreateParameter("@Email",t.Email),
                DataAccess.CreateParameter("@CreateOn",t.CreateOn)};
        }

        NewsletterInfo builder_CreateModel(Hashtable reader){

            NewsletterInfo model = new NewsletterInfo();

            model.Id = reader.Get<int>("Id");
            model.Email = reader.Get<string>("Email");
            model.CreateOn = reader.Get<DateTime>("CreateOn");

            return model;
        }

        #endregion

        #region INewsletterDAL

        public NewsletterInfo FindById(int id)
        {
            return this.builder.GetModel(id);
        }

        public IList<NewsletterInfo> FindAll(int pageIndex, int pageSize, out int totalRecord)
        {
            return this.builder.GetModels(string.Empty, string.Empty, "order by Id asc", pageIndex, pageSize, out totalRecord, null);
        }

        public void Add(NewsletterInfo currency)
        {
            this.builder.Insert(
               currency,
               "Email",
               "CreateOn");
        }

        public void Update(NewsletterInfo currency)
        {
            this.builder.Update(
                currency,
                "Id=@Id",
                "Email",
                "CreateOn");
        }

        public void Delete(int id)
        {
            this.builder.Delete(id);

        }

        #endregion
    }
}
